﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SubSonic;
using CXDB;
using Coolite.Ext.Web;

public partial class Company_UserList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            Store1Databind();
            Store2Databind();
        } 
        
    }
    protected void Store1_OnRefreshData(object s,StoreRefreshDataEventArgs e)
    {
        Store1Databind();
    }
    protected void Store1Databind()
    {
        Query q=new Query(VwCompanybase.Schema);
        q.AddWhere("depid", Cookies.getCookies("cdeptid"));
        q.SetSelectList("companyid,companyname,electricname,deptname,scores,credit,tradename");
        Store1.DataSource = q.ExecuteDataSet();
        Store1.DataBind();
    }
    protected void Store2Databind()
    {
        Query q = new Query(CxUserBase.Schema);
        q.SetSelectList("username,uid");
        q.AddWhere("depid", getDeptId(int.Parse(Cookies.getCookies("cdeptid"))));
        q.AddWhere("levelid", 1);
        Store2.DataSource = q.ExecuteDataSet();
        Store2.DataBind();
    }
    [AjaxMethod]
    protected void RowSelect(object s,AjaxEventArgs e)
     {
         string companyid = e.ExtraParams["companyid"];
         tfid.Value = companyid;
         tfcompanyid.Text = companyid;
        Query q = new Query(CxCompanyBase.Schema);
        q.AddWhere("companyid", companyid);
        q.SetSelectList("companyname");
        tfcompanyname.Text = q.ExecuteScalar().ToString();
        tfcompanyname2.Value = tfcompanyname.Text;
     }
    [AjaxMethod]
    protected void SubForm(object s,AjaxEventArgs e)
    {
        if (tfid.Value.ToString() == "")
        {
            Err.Warning("请从左侧列表选择一条记录！", MessageBox.Icon.WARNING);
            return;
        }
        if (tfdept_master_id.SelectedIndex == -1)
        {
            Err.Warning("请选择部门领导！", MessageBox.Icon.WARNING);
            return;
        }
        
        Guid g = Guid.NewGuid();
        
        CxCompanyPingJium cx= new CxCompanyPingJium();
        cx.CompanyID = tfid.Value.ToString();
        cx.CompanyName = tfcompanyname2.Value.ToString();

        
        if(tfunfee_time.Value.ToString() !="0001/1/1 0:00:00")
        {
            cx.UnFeeTime = DateTime.Parse(tfunfee_time.Value.ToString());
            cx.UnFeeCash = int.Parse(tfunfee_cash.Value.ToString());
            cx.UnFeeDetail = tfunfee_detail.Text;
        }
        if ((tfthief_time1.Value.ToString() != "0001/1/1 0:00:00") && (tfthief_time2.Value.ToString() != "0001/1/1 0:00:00")&&(DateTime.Parse(tfthief_time1.Value.ToString())<DateTime.Parse(tfthief_time2.Value.ToString())))
        {
            cx.ThiefTime1 = DateTime.Parse(tfthief_time1.Value.ToString());
            cx.ThiefTime2 = DateTime.Parse(tfthief_time2.Value.ToString());
            cx.ThiefCash = int.Parse(tfunfee_cash.Value.ToString());
            cx.ThiefDetail = tfthief_detail.Text;
        }
        if (tfuncoop_peak_time.Value.ToString() != "0001/1/1 0:00:00")
        {
            cx.UnCoopPeakTime = DateTime.Parse(tfuncoop_peak_time.Value.ToString());
            cx.UnCoopPeakDetail = tfuncoop_peak_detail.Text;
        }
        if (tfuncoop_limit_time.Value.ToString() != "0001/1/1 0:00:00")
        {
            cx.UnCoopLimitTime = DateTime.Parse(tfuncoop_limit_time.Value.ToString());
            cx.UnCoopLimitDetail = tfuncoop_limit_detail.Text;
        }
        cx.EnterUserId = int.Parse(Cookies.getCookies("cuid"));
        cx.DeptMasterId = int.Parse(tfdept_master_id.SelectedItem.Value.ToString());
        cx.ManagerStatus = 0;
        cx.DeptMasterStatus = 0;
        //cx.OtherMasterStatus = 0;
        cx.UnCoopOthers = tfuncoop_others.Text;
        cx.InTime = DateTime.Now;
        cx.Guid = g;
        cx.Save();
        CXFileUpload.Sv(tffileupload,g);

        tfid.Value = "";
        tfunfee_time.Value = "";
        tfunfee_detail.Text = "";
        tfunfee_cash.Clear();
        tfuncoop_peak_time.Value = "";
        tfuncoop_peak_detail.Text = "";
        tfuncoop_others.Text = "";
        tfuncoop_limit_time.Value = "";
        tfuncoop_limit_detail.Text = "";
        tfthief_time2.Value = "";
        tfthief_time1.Value = "";
        tfthief_detail.Text = "";
        tfthief_cash.Clear();
        tfdept_master_id.SelectedIndex = -1;
        tfcompanyname.Text = "";
        tfcompanyid.Text = "";
        tfdept_master_id.Reset();

        Err.Warning("提交成功！", MessageBox.Icon.NONE);
    }
    protected int getDeptId(int deptId)
    {
        if (deptId > 10)
        {
            Query q = new Query(CxSysDept.Schema);
            q.SetSelectList("deptid");
            q.AddWhere("ID", deptId);
            deptId = int.Parse(q.ExecuteScalar().ToString());
        }
        return deptId;
    }
}
